﻿Public Class DatabaseManager

    Private Shared _DbProvider As IDbProvider
    Private Shared _ConnectionString As String
    Private Shared _DbType As DatabaseEnum

    Public Enum DatabaseEnum
        dbUnknown
        dbNone = 0
        dbSqlServer
        dbOracle
        dbMySql
        dbPostGres
        dbODBC
    End Enum

    Private Sub New()
        '***
    End Sub

    Shared Sub New()
        Dim sCnnToUse As String = Configuration.ConfigurationManager.AppSettings.Item("CnnToUse")
        With Configuration.ConfigurationManager.ConnectionStrings(sCnnToUse)
            Dim sCnn As String = .ConnectionString

            _ConnectionString = sCnn
            Select Case .ProviderName
                Case "System.Data.SqlClient"
                    _DbType = DatabaseEnum.dbSqlServer

                Case "System.Data.OracleDB"
                    _DbType = DatabaseEnum.dbOracle

                Case Else
                    _DbType = DatabaseEnum.dbODBC
            End Select

        End With
        _DbProvider = GetProvider(_DbType, _ConnectionString)
    End Sub

    Private Shared Function GetProvider(ByVal DatabaseType As DatabaseEnum, ByVal ConnectionString As String) As IDbProvider
        Dim ret As IDbProvider = Nothing
        Select Case DatabaseType
            Case DatabaseEnum.dbUnknown
                '*** do something clever.

            Case DatabaseEnum.dbSqlServer
                ret = New ProviderSqlServer

            Case DatabaseEnum.dbOracle
                '_DbProvider= New dbOracle

            Case DatabaseEnum.dbMySql
                '_DbProvider= New dbMySqlAdapter

            Case DatabaseEnum.dbPostGres
                '_DbProvider= New dbPostGres

            Case DatabaseEnum.dbOracle
                '_DbProvider= New dbOracle

        End Select
        If Not ret Is Nothing Then
            ret.ConnectionString = ConnectionString
        End If
        Return ret

    End Function

    Public Shared ReadOnly Property ConnectionString() As String
        Get
            Return _ConnectionString
        End Get
    End Property

    Public Shared ReadOnly Property DbType() As DatabaseEnum
        Get
            Return _DbType
        End Get
    End Property


    'Run stored proecures with and without parameters.
    Public Shared Function StoredProc(ByVal spName As String, ByVal UserId As Integer) As Integer
        Return _DbProvider.StoredProc(spName, UserId)
    End Function

    Public Shared Function StoredProc(ByVal spName As String, ByVal params As List(Of IDataParameter), ByVal UserId As Integer) As Integer
        Return _DbProvider.StoredProc(spName, params, UserId)
    End Function

    Public Shared Function StoredProc(ByVal spName As String, ByVal result As DataTable, ByVal UserId As Integer) As Integer
        Return _DbProvider.StoredProc(spName, result, UserId)
    End Function

    Public Shared Function StoredProc(ByVal spName As String, ByVal params As List(Of IDataParameter), ByVal result As DataTable, ByVal UserId As Integer) As Integer
        Return _DbProvider.StoredProc(spName, params, result, UserId)
    End Function

    Public Shared Function DataAdapter() As IDataAdapter
        Return _DbProvider.DataAdapter
    End Function

    Public Shared Function DataReader() As IDataReader
        Return _DbProvider.DataReader
    End Function

    Public Shared Function Parameter() As IDataParameter
        Return _DbProvider.Parameter
    End Function

    Public Shared Function OutputParameter(ByVal name As String, ByVal type As DbType) As IDataParameter

        Dim oParam As IDataParameter = _DbProvider.Parameter
        With oParam
            .DbType = type
            .Direction = ParameterDirection.Output
            .ParameterName = name
        End With

        Return oParam

    End Function

    Public Shared Function InputParameter(ByVal Name As String, ByVal Type As DbType, ByVal Value As Object) As IDataParameter

        Dim oParam As IDataParameter = _DbProvider.Parameter
        With oParam
            .DbType = Type
            .Direction = ParameterDirection.Input
            .ParameterName = Name
            .Value = Value
        End With

        Return oParam

    End Function


End Class
